//
//  HBChapterController.m
//  HeartBeat
//
//  Created by karong on 2023/8/31.
//

#import "HBChapterController.h"
#import "HBChapterModel.h"
#import "HBChapterCell.h"
#import "HBChapterHeadView.h"
#import "HBChapterRequest.h"
@interface HBChapterController () <GRChapterHeaderViewDelegate>

@property (weak, nonatomic) IBOutlet UICollectionView *listView;
@property (nonatomic, strong) HBChaptersModel * chapters;
@property (nonatomic, strong) HBChapterRequest * request;

@end

@implementation HBChapterController

- (void)viewDidLoad {
    [super viewDidLoad];
    HBChapterRequest * request = [HBChapterRequest new];
    request.novel_id = self.novel.book_id;
    request.sort_type = 1;
    self.request = request;
}

- (void)loadSuccessWithArray:(NSArray *)array isRefresh:(BOOL)isRefresh {
    self.chapters = self.request.chapters;
    [super loadSuccessWithArray:array isRefresh:isRefresh];
}

- (BOOL)disableAutoBeginRefresh {
    return YES;
}


- (IBAction)closeAction:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark UICollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HBChapterCell * chapterCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HBChapterCell" forIndexPath:indexPath];
    [chapterCell setDataWithObject:self.dataArray[indexPath.row] chapterId:self.novel.chapter_id];
    return chapterCell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGSize size = CGSizeZero;
    size.width = HBScreenWidth;
    size.height = 55.5 + (appConfig.languageType == 2?20:0);
//    size.height = 55.5;
    return size;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        HBChapterHeadView * chapterHeadView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"HBChapterHeadView" forIndexPath:indexPath];
            [chapterHeadView setDataWithTotal:self.chapters.total sort_type:self.request.sort_type];
        chapterHeadView.delegate = self;
            return chapterHeadView;
    }
    return nil;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    CGSize size = CGSizeZero;
    size.width = HBScreenWidth;
    size.height = 40;
    return size;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [self dismissViewControllerAnimated:YES completion:^{
        if (self.selectHandle) {
            self.selectHandle(self.dataArray[indexPath.row]);
        }
    }];
    
}

#pragma mark GRChapterHeaderViewDelegate
- (void)chapterSortAction:(BOOL)reverse {
    self.request.sort_type = reverse?2:1;
    [self benginRefresh];
}

@end
